/*******************************************************************************
 * BannerAdvertisersAction.java
 *
 * Created by Nishan
 * Last modified Date: Oct 24, 2008 4:52:35 PM by nishan
  * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ******************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.HibernateException;

import com.printeverywhere.delegator.BannerAccountsServiceDelegator;
import com.printeverywhere.delegator.BannerCampaignServiceDelegator;
import com.printeverywhere.delegator.BannerZonesServiceDelegator;
import com.printeverywhere.dto.BannerAccount;
import com.printeverywhere.dto.BannerCampaigns;
import com.printeverywhere.dto.BannerDailyStatistics;
import com.printeverywhere.dto.BannerHourlyStatistics;
import com.printeverywhere.dto.BannerSettings;
import com.printeverywhere.dto.BannerZones;
import com.printeverywhere.util.SearchCriteria;

// TODO: Auto-generated Javadoc
/**
 * The Class BannerAdvertisersAction.
 */
public class BannerAdvertisersAction extends FrontBaseSupport implements
		ServletRequestAware {

	/**
	 * Instantiates a new banner advertisers action.
	 */
	public BannerAdvertisersAction() {
		super(true, false);
		setMyAccount(true);
	}

	/** Logger for this class. */
	private static final Logger logger = Logger
			.getLogger(BannerAdvertisersAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The request. */
	private HttpServletRequest request;

	/** The banner account list. */
	private List<BannerAccount> bannerAccountList;

	/** The banner campaign list. */
	private List<BannerCampaigns> bannerCampaignList;

	/** The banner zones list. */
	private List<BannerZones> bannerZonesList;

	/** The banner account. */
	private BannerAccount bannerAccount;

	/** The banner campaigns. */
	private BannerCampaigns bannerCampaigns;

	/** The banner zone. */
	private BannerZones bannerZone;

	/** The str banner zones. */
	private String strBannerZones;

	/** The banner settings. */
	private BannerSettings bannerSettings;

	/** The banner list. */
	private List<BannerCampaigns> bannerList;

	/** The statistic type. */
	private List<String> statisticType;

	/** The campaign id. */
	private int campaignId;

	/** The type. */
	private String type;

	/** The daily statistics list. */
	private List<BannerDailyStatistics> dailyStatisticsList;

	/** The hourly statistics list. */
	private List<BannerHourlyStatistics> hourlyStatisticsList;

	/** The banners map. */
	private Map<Integer, BannerCampaigns> bannersMap;

	/**
	 * Gets the hourly statistics list.
	 *
	 * @return the hourlyStatisticsList
	 */
	public List<BannerHourlyStatistics> getHourlyStatisticsList() {
		return hourlyStatisticsList;
	}

	/**
	 * Sets the hourly statistics list.
	 *
	 * @param hourlyStatisticsList the hourlyStatisticsList to set
	 */
	public void setHourlyStatisticsList(
			List<BannerHourlyStatistics> hourlyStatisticsList) {
		this.hourlyStatisticsList = hourlyStatisticsList;
	}

	/**
	 * Gets the daily statistics list.
	 *
	 * @return the dailyStatisticsList
	 */
	public List<BannerDailyStatistics> getDailyStatisticsList() {
		return dailyStatisticsList;
	}

	/**
	 * Sets the daily statistics list.
	 *
	 * @param dailyStatisticsList the dailyStatisticsList to set
	 */
	public void setDailyStatisticsList(
			List<BannerDailyStatistics> dailyStatisticsList) {
		this.dailyStatisticsList = dailyStatisticsList;
	}

	/**
	 * Gets the type.
	 *
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * Sets the type.
	 *
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * Gets the campaign id.
	 *
	 * @return the campaignId
	 */
	public int getCampaignId() {
		return campaignId;
	}

	/**
	 * Sets the campaign id.
	 *
	 * @param campaignId the campaignId to set
	 */
	public void setCampaignId(int campaignId) {
		this.campaignId = campaignId;
	}

	/**
	 * Gets the banner settings.
	 *
	 * @return the bannerSettings
	 */
	public BannerSettings getBannerSettings() {
		return bannerSettings;
	}

	/**
	 * Sets the banner settings.
	 *
	 * @param bannerSettings the bannerSettings to set
	 */
	public void setBannerSettings(BannerSettings bannerSettings) {
		this.bannerSettings = bannerSettings;
	}

	/**
	 * Gets the str banner zones.
	 *
	 * @return the str banner zones
	 */
	public String getStrBannerZones() {
		return strBannerZones;
	}

	/**
	 * Sets the str banner zones.
	 *
	 * @param strBannerZones the new str banner zones
	 */
	public void setStrBannerZones(String strBannerZones) {
		this.strBannerZones = strBannerZones;
	}

	/**
	 * Gets the banner campaigns.
	 *
	 * @return the banner campaigns
	 */
	public BannerCampaigns getBannerCampaigns() {
		return bannerCampaigns;
	}

	/**
	 * Sets the banner campaigns.
	 *
	 * @param bannerCampaigns the new banner campaigns
	 */
	public void setBannerCampaigns(BannerCampaigns bannerCampaigns) {
		this.bannerCampaigns = bannerCampaigns;
	}

	/**
	 * Gets the banner account.
	 *
	 * @return the banner account
	 */
	public BannerAccount getBannerAccount() {
		return bannerAccount;
	}

	/**
	 * Sets the banner account.
	 *
	 * @param bannerAccount the new banner account
	 */
	public void setBannerAccount(BannerAccount bannerAccount) {
		this.bannerAccount = bannerAccount;
	}

	/**
	 * Gets the banner account list.
	 *
	 * @return the banner account list
	 */
	public List<BannerAccount> getBannerAccountList() {
		return bannerAccountList;
	}

	/**
	 * Sets the banner account list.
	 *
	 * @param bannerAccountList the new banner account list
	 */
	public void setBannerAccountList(List<BannerAccount> bannerAccountList) {
		this.bannerAccountList = bannerAccountList;
	}

	/**
	 * Gets the banner campaign list.
	 *
	 * @return the banner campaign list
	 */
	public List<BannerCampaigns> getBannerCampaignList() {
		return bannerCampaignList;
	}

	/**
	 * Sets the banner campaign list.
	 *
	 * @param bannerCampaignList the new banner campaign list
	 */
	public void setBannerCampaignList(List<BannerCampaigns> bannerCampaignList) {
		this.bannerCampaignList = bannerCampaignList;
	}

	/**
	 * Gets the banner zones list.
	 *
	 * @return the banner zones list
	 */
	public List<BannerZones> getBannerZonesList() {
		return bannerZonesList;
	}

	/**
	 * Sets the banner zones list.
	 *
	 * @param bannerZonesList the new banner zones list
	 */
	public void setBannerZonesList(List<BannerZones> bannerZonesList) {
		this.bannerZonesList = bannerZonesList;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		return SUCCESS;
	}

	/**
	 * Show add new account.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String showAddNewAccount() throws Exception {
		return SUCCESS;
	}

	/**
	 * Creates the banner account.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String createBannerAccount() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("createBannerAccount() - start"); //$NON-NLS-1$
		}
		try {
			BannerAccountsServiceDelegator.inserBannerAccount(bannerAccount);
		} catch (HibernateException e) {
			return INPUT;
		}

		if (logger.isDebugEnabled()) {
			logger.debug("createBannerAccount() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Show add new campaign.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String showAddNewCampaign() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("showAddNewCampaign() - start"); //$NON-NLS-1$
		}

		this.bannerAccountList = BannerAccountsServiceDelegator
				.getBannerAccounts();

		if (logger.isDebugEnabled()) {
			logger.debug("showAddNewCampaign() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Creates the campaign.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String createCampaign() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("createCampaign() - start"); //$NON-NLS-1$
		}

		BannerCampaignServiceDelegator.inserBannerCampaigns(bannerCampaigns);

		if (logger.isDebugEnabled()) {
			logger.debug("createCampaign() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Show manage accounts.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String showManageAccounts() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("showManageAccounts() - start"); //$NON-NLS-1$
		}

		this.bannerAccountList = BannerAccountsServiceDelegator
				.getBannerAccounts();
		int totalBannerAccount = this.bannerAccountList.size();
		for (int index = 0; index < totalBannerAccount; index++) {
			BannerAccount bannerAccount = this.bannerAccountList.get(index);
			bannerAccount.setTotalCampaigns(BannerCampaignServiceDelegator
					.countBannerCampaigns(bannerAccount.getId()));
			this.bannerAccountList.set(index, bannerAccount);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("showManageAccounts() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Show manage campaigns.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String showManageCampaigns() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("showManageCampaigns() - start"); //$NON-NLS-1$
		}

		this.bannerCampaignList = BannerCampaignServiceDelegator
				.getBannerCampaigns();

		if (logger.isDebugEnabled()) {
			logger.debug("showManageCampaigns() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Show banner settings.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String showBannerSettings() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("showBannerSettings() - start"); //$NON-NLS-1$
		}

		bannerSettings = BannerCampaignServiceDelegator.getBannerSettings();

		if (logger.isDebugEnabled()) {
			logger.debug("showBannerSettings() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Edits the banner settings.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String editBannerSettings() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("showBannerSettings() - start"); //$NON-NLS-1$
		}

		BannerCampaignServiceDelegator.editBannerSetting(bannerSettings);

		if (logger.isDebugEnabled()) {
			logger.debug("showBannerSettings() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Show banner statistics.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String showBannerStatistics() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("showBannerStatistics() - start"); //$NON-NLS-1$
		}

		populateBannerSearchList();

		if (logger.isDebugEnabled()) {
			logger.debug("showBannerStatistics() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Gets the banner statistics.
	 *
	 * @return the banner statistics
	 *
	 * @throws Exception the exception
	 */
	public String getBannerStatistics() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getBannerStatistics() - start"); //$NON-NLS-1$
		}
		populateBannerSearchList();
		SearchCriteria searchCriteria = new SearchCriteria();
		if (campaignId == 0) {
			addActionError("Please Select Campaign ");
			return INPUT;
		} else if (type.equalsIgnoreCase("daily")) {
			searchCriteria.setAttribute("bannerCampaigns.id", campaignId);
			dailyStatisticsList = BannerCampaignServiceDelegator.getDailyStatisticsList(searchCriteria);

		} else if (type.equalsIgnoreCase("hourly")) {
			searchCriteria.setAttribute("bannerCampaigns.id", campaignId);
			hourlyStatisticsList = BannerCampaignServiceDelegator.getHourlyStatisticsList(searchCriteria);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getBannerStatistics() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Populate banner search list.
	 */
	public void populateBannerSearchList() {
		if (logger.isDebugEnabled()) {
			logger.debug("populateBannerSearchList() - start"); //$NON-NLS-1$
		}
		this.bannersMap = new HashMap<Integer, BannerCampaigns>();
		this.bannerList = BannerCampaignServiceDelegator.getBannerCampaigns();

		for (BannerCampaigns bannersCampaigns : bannerList) {
			this.bannersMap.put(bannersCampaigns.getId(), bannersCampaigns);
		}

		statisticType = new ArrayList<String>();
		statisticType.add("DAILY");
		statisticType.add("HOURLY");
		statisticType.add("WEEKLY");
		statisticType.add("MONTHLY");
		statisticType.add("YEARLY");
		if (logger.isDebugEnabled()) {
			logger.debug("populateBannerSearchList() - end"); //$NON-NLS-1$
		}
	}

	/**
	 * Show campaigns.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String showCampaigns() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("showCampaigns() - start"); //$NON-NLS-1$
		}
		populateBannerSearchList();

		if (logger.isDebugEnabled()) {
			logger.debug("showCampaigns() - end"); //$NON-NLS-1$
		}
		return SUCCESS;

	}

	/**
	 * Enable campaigns.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String enableCampaigns() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("enableCampaigns() - start"); //$NON-NLS-1$
		}

		for (BannerCampaigns changeBannerStatus : getBannersMap().values()) {
			if (changeBannerStatus.getStatus() == null) {
				changeBannerStatus.setStatus(false);
			}
			BannerCampaignServiceDelegator.updateBannerCampaigns(changeBannerStatus);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("enableCampaigns() - end"); //$NON-NLS-1$
		}

		populateBannerSearchList();

		return SUCCESS;
	}

	/**
	 * Show manage zones.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String showManageZones() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("showManageZones() - start"); //$NON-NLS-1$
		}

		this.bannerZonesList = BannerZonesServiceDelegator.getBannerZones();

		if (logger.isDebugEnabled()) {
			logger.debug("showManageZones() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Show zones configuration.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String showZonesConfiguration() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("showZonesConfiguration() - start"); //$NON-NLS-1$
		}

		if (this.request.getParameter("id") != null) {
			this.bannerZone = BannerZonesServiceDelegator
					.getBannerZoneById(Integer.parseInt(this.request
							.getParameter("id")));
			this.bannerZonesList = BannerZonesServiceDelegator.getBannerZones();
			this.bannerCampaignList = BannerCampaignServiceDelegator
					.getBannerCampaigns();
		}

		if (logger.isDebugEnabled()) {
			logger.debug("showZonesConfiguration() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Update zone configuration.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String updateZoneConfiguration() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("updateZoneConfiguration() - start"); //$NON-NLS-1$
		}

		BannerZonesServiceDelegator.updateBannerZones(this.bannerZone);

		if (logger.isDebugEnabled()) {
			logger.debug("updateZoneConfiguration() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Creates the zones.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String createZones() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("createZones() - start"); //$NON-NLS-1$
		}

		String[] zones = this.strBannerZones.split(",");

		for (String zone : zones) {
			if (zone.trim().length() > 1) {
				BannerZones bannerZones = new BannerZones();
				bannerZones.setZone(zone);
				BannerZonesServiceDelegator.inserBannerZones(bannerZones);
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("createZones() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Delete zones.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String deleteZones() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteZones() - start"); //$NON-NLS-1$
		}

		try {
			if (this.request.getParameter("id") != null) {
				this.bannerZone = BannerZonesServiceDelegator
						.getBannerZoneById(Integer.parseInt(this.request
								.getParameter("id")));
				BannerZonesServiceDelegator.deleteBannerZones(this.bannerZone);
			}

			if (logger.isDebugEnabled()) {
				logger.debug("deleteZones() - end"); //$NON-NLS-1$
			}
			return SUCCESS;
		} catch (Exception e) {
			logger.error("deleteZones()", e); //$NON-NLS-1$

			addActionMessage("Can't delete this record...");
			this.bannerZonesList = BannerZonesServiceDelegator.getBannerZones();
		}

		if (logger.isDebugEnabled()) {
			logger.debug("deleteZones() - end"); //$NON-NLS-1$
		}
		return INPUT;
	}

	/**
	 * Gets the banner zone.
	 *
	 * @return the banner zone
	 */
	public BannerZones getBannerZone() {
		return bannerZone;
	}

	/**
	 * Sets the banner zone.
	 *
	 * @param bannerZone the new banner zone
	 */
	public void setBannerZone(BannerZones bannerZone) {
		this.bannerZone = bannerZone;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * Gets the statistic type.
	 *
	 * @return the statisticType
	 */
	public List<String> getStatisticType() {
		return statisticType;
	}

	/**
	 * Sets the statistic type.
	 *
	 * @param statisticType the statisticType to set
	 */
	public void setStatisticType(List<String> statisticType) {
		this.statisticType = statisticType;
	}

	/**
	 * @return the bannerList
	 */
	public List<BannerCampaigns> getBannerList() {
		return bannerList;
	}

	/**
	 * @param bannerList the bannerList to set
	 */
	public void setBannerList(List<BannerCampaigns> bannerList) {
		this.bannerList = bannerList;
	}

	/**
	 * @return the bannersMap
	 */
	public Map<Integer, BannerCampaigns> getBannersMap() {
		return bannersMap;
	}

	/**
	 * @param bannersMap the bannersMap to set
	 */
	public void setBannersMap(Map<Integer, BannerCampaigns> bannersMap) {
		this.bannersMap = bannersMap;
	}


}